﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using Telerik.Web.UI;
using KPIS.Utils;
using System.Web.UI.HtmlControls;
using KPIS.GERP.GHRMS.MODEL;
using System.Collections;
using System.Globalization;
using KPIS.GERP.GHRMS.APP.Classes;

namespace KPIS.GERP.GHRMS.APP.App_UI.Forms
{
    public partial class PR_BonusCommandDtls : System.Web.UI.Page
    {
        private SsMng ssMng = new SsMng();
        IEmployeeBLL empBLL = new EmployeeBLL();
        IEmployeeTypeBLL empTypeBLL = new EmployeeTypeBLL();
        IPositionTypeBLL posTypeBLL = new PositionTypeBLL();
        SystemYearTypeBLL yearTypeBLL = new SystemYearTypeBLL();
        AppraisalsBLL appraisalsBLL = new AppraisalsBLL();
        SalaryPromotionConfigBLL promotionManageBLL = new SalaryPromotionConfigBLL();
       // Command_BLL cmdBLL = new Command_BLL();
        Command_BLL cmdBLL = new Command_BLL();
        Main main = new Main();

        protected void Page_Load(object sender, EventArgs e)
        {
            IncludeJava();
            txtExAmount.Attributes.Add("onkeypress", "return isNumberKeyDot(event)");
            if (!IsPostBack)
            {
                lblID.Text = Request.QueryString["id"];
                bindEmpType();
            }
            
        }
        public void IncludeJava()
        {
            HtmlGenericControl script = new HtmlGenericControl();
            script.TagName = "script";
            script.Attributes.Add("type", "text/javascript");
            script.Attributes.Add("src", "../../Scripts/jsfunction.js");
            Page.Header.Controls.Add(script);
        }
        private void bindEmpType()
        {
            IList<EmployeeTypeInfo> infoList = empTypeBLL.ListEmployeeTypes();
            ddlEmployeeType.DataSource = infoList;
            ddlEmployeeType.DataBind();
        }

        public void ListYear(int MinYear, int MaxYear)
        {
            DataTable dt = new DataTable();
            DataColumn dc = dt.Columns.Add("Name");

            for (int i = MinYear; i <= MaxYear; i++)
            {
                dt.Rows.Add(Convert.ToString(i));
            }
            ddlYear.DataSource = dt;
            ddlYear.DataBind();
          //  ddlYear.SelectedValue = Convert.ToString(Int32.Parse(DateTime.Today.Year.ToString()) + 543);
        }

        protected void ddlEmployeeType_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            try
            {
                Session["id"] = lblID.Text + "|" + ddlEmployeeType.SelectedValue;

                DataSet ds = yearTypeBLL.GetYearType(int.Parse(ddlEmployeeType.SelectedValue));
                lblTypeYear.Text = ds.Tables[0].Rows[0]["year_type_name"].ToString();
                int myYearNow = DateTime.Today.Year+543;
                ListYear(myYearNow - 10, myYearNow + 10);
                ddlYear.ClearSelection();
                ddlYear.Text = string.Empty;
            }
            catch
            {
                lblTypeYear.Text = "";
                ddlYear.ClearSelection();
                ddlYear.DataSource = null;
                ddlYear.Text = string.Empty;
            }
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            LoadDataGridCondition();
        }


        private void LoadDataGridCondition()
        {
            try
            {
                DataSet ds = posTypeBLL.ListPosionTypeClass(int.Parse(ddlEmployeeType.SelectedValue));
                RadGrid3.DataSource = ds.Tables[0];
                RadGrid3.DataBind();
            }
            catch (Exception ex)
            {
                RadGrid3.DataSource = null;
                RadGrid3.DataBind();
            }
        }

        protected void RadGrid3_ItemDataBound(object sender, GridItemEventArgs e)
        {

        }
        protected void RadGrid3_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "ExpandCollapse")
                return;
            LoadDataGridCondition();
        }



        private string filterClass()
        {
            CheckBox cb2;
            ArrayList arr = new ArrayList();
            foreach (GridItem item in this.RadGrid3.Items)
            {
               // string ss = item.Cells[3].ToString();
                cb2 = (CheckBox)item.Cells[2].FindControl("cbClass");
                if (cb2.Checked == true)
                {
                    arr.Add(item.OwnerTableView.DataKeyValues[item.ItemIndex]["class_seq"].ToString());
                }

            }
            string[] strArray = arr.ToArray(typeof(string)) as string[];
            string str = string.Join(",", strArray);
            return str.Trim();
        }


        protected void btnFilter_Click(object sender, EventArgs e)
        {
            if (filterClass() == "")
            {
                main.MessageAlert(this.Page, "กรุณาระบุเงื่อนไข!!");
                return;
            }
            bindCompare();
        }

        protected void RadGrid4_ItemCommand(object source, GridCommandEventArgs e)
        {
            RadGrid4.DataSource = (DataTable)Session["dtForSave"];
            RadGrid4.DataBind();
        }

        protected void RadGrid4_ItemDataBound(object sender, GridItemEventArgs e)
        {
            //duplicated
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                CheckBox cb4;
                if (e.Item.Cells[3].Text == "Y")
                {
                    GridDataItem item = (GridDataItem)e.Item;  
                    cb4 = (CheckBox)item.Cells[2].FindControl("cb4");
                    cb4.Checked = true;
                    cb4.Enabled = false;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtExAmount.Text == "")
            {
                main.MessageAlert(this.Page, "กรุณาระบุเงินรางวัล!!");
                return;
            }
            CheckBox cb4;
            ArrayList arr = new ArrayList();
            int countGrdIsCheck = 0;
            foreach (GridItem item in this.RadGrid4.Items)
            {
                cb4 = (CheckBox)item.Cells[2].FindControl("cb4");
                if (cb4.Checked == true && cb4.Enabled == true)
                {
                    arr.Add(item.OwnerTableView.DataKeyValues[item.ItemIndex]["Sequence"].ToString());
                    countGrdIsCheck++;
                    cb4.Enabled = false;
                }
            }

            DataTable dt = (DataTable)Session["dtForSave"];
            for (int i = 0; i < countGrdIsCheck; i++)
            {
                DataRow dr;
                dr = dt.Select("Sequence =" + arr[i])[0];
                cmdBLL.InsertBonusCommandDetail(lblID.Text
                    , "1"
                    , dr["sequence"].ToString()
                    , dr["t_pos_seq"].ToString()
                    , dr["pos_ref"].ToString()
                    , dr["pos_name"].ToString()
                    , dr["t_class_seq"].ToString()
                    , dr["working_job_site_seq"].ToString()
                    , dr["job_site_name"].ToString()
                    , dr["salary"].ToString()
                    , txtExAmount.Text
                    , txtRemark.Text
                    , ssMng.EmpSeq);
            }
            txtExAmount.Text = "";
            txtRemark.Text = "";
            RadTabStrip1.SelectedIndex = 2;
            RadMultiPage1.SelectedIndex = 2;
            bindDataPrint();
        }

        private void bindCompare()
        {
            RadTabStrip1.SelectedIndex = 1;
            RadMultiPage1.SelectedIndex = 1;

            DataTable dt = null;// empBLL.ListEmployeeByTypes(ddlEmployeeType.SelectedValue, filterClass());
            //hrm_pr_command_dtls
            DataTable dt2 = cmdBLL.ListBonusCommandDetail(lblID.Text).Tables[0];
            dt.Columns.Add("duplicated", typeof(string));

            foreach (DataRow dr in dt.Rows)
            {
                foreach (DataRow dr2 in dt2.Rows)
                {
                    if (dr["emp_seq"].ToString() == dr2["emp_seq"].ToString())
                    {
                        dr["duplicated"] = "Y";
                        break;
                    }
                    else
                        dr["duplicated"] = "N";
                }
            }

            dt.AcceptChanges();
            Session["dtForSave"] = dt;

            RadGrid4.DataSource = dt;
            RadGrid4.DataBind();
            if (dt.Rows.Count != 0)
                btnSave.Visible = true;
            else
                btnSave.Visible = false;

            bindDataPrint();
        }


        private void bindDataPrint()
        {
            DataTable dt = new DataTable();
            dt = cmdBLL.ListSalaryCommand(lblID.Text).Tables[0];
            lblCmdName.Text = dt.Rows[0]["cmd_name"].ToString();
            lblRefNo.Text = dt.Rows[0]["cmd_ref_no"].ToString();
            lblAppDate.Text = ((DateTime)dt.Rows[0]["approved_date"]).ToString("dd MMMM yyyy", new CultureInfo("th-TH"));

            DataSet ds = new DataSet();
           // ds = (DataSet)Session["ds"];
            ds = cmdBLL.ListBonusCommandDetail(lblID.Text);
            RadGrid1.DataSource = ds;
            RadGrid1.DataBind();
            RadGrid1.Visible = true;
            if (ds.Tables[0].Rows.Count != 0)
                btnPrint.Visible = true;
            else
                btnPrint.Visible = false;
        }

        protected void RadGrid1_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Delete")
            {
                int seq = Convert.ToInt32((e.Item as GridDataItem).OwnerTableView.DataKeyValues[e.Item.ItemIndex]["cmd_ext_seq"]);
                cmdBLL.DeleteBonusCommandDetail(seq, ssMng.EmpSeq);
            }
            if (e.CommandName == "btnClick")
            {
                return;
            }
            bindCompare();
        }


        

    }
}
